/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */

package org.ponly.cta4j.factory.gimpy;

import org.ponly.cta4j.CaptchaFactory;
import org.ponly.cta4j.drawing.FilterablePainter;
import org.ponly.cta4j.drawing.Painter;
import org.ponly.cta4j.drawing.background.BackgroundBrush;
import org.ponly.cta4j.drawing.background.ImageSourceBackgroundBrush;
import org.ponly.cta4j.drawing.color.RandomColorGenerator;
import org.ponly.cta4j.drawing.color.SingleColorGenerator;
import org.ponly.cta4j.drawing.filter.BufferedImageOpFilter;
import org.ponly.cta4j.drawing.filter.Filter;
import org.ponly.cta4j.drawing.font.FontFamily;
import org.ponly.cta4j.drawing.font.RandomFontFamily;
import org.ponly.cta4j.drawing.text.GlyphsBrush;
import org.ponly.cta4j.drawing.text.TextBrush;
import org.ponly.cta4j.drawing.text.glyphsdecorator.GlyphsDecorator;
import org.ponly.cta4j.drawing.text.glyphsdecorator.RandomLinesGlyphsDecorator;
import org.ponly.cta4j.drawing.text.glyphsvisitor.*;
import org.ponly.cta4j.factory.ProxiedCaptchaFactory;

import java.awt.*;

/**
 * 使用图片背景风格的验证码工厂
 *
 * @author vacoor
 * @since 1.0
 */
public class DefaultImageBackgroundFactory extends ProxiedCaptchaFactory {
    /**
     * 默认实例.
     */
    public static final DefaultImageBackgroundFactory INSTANCE = new DefaultImageBackgroundFactory();

    /**
     * captcha width.
     */
    private static final int WIDTH = 100;

    /**
     * captcha height.
     */
    private static final int HEIGHT = 38;

    private static final int MIN_TEXT_LENGTH = 4;
    private static final int MAX_TEXT_LENGTH = MIN_TEXT_LENGTH;
    private static final int MIN_FONT_SIZE = 30;
    private static final int MAX_FONT_SIZE = MIN_FONT_SIZE;

    private GimpyFactory factory = createFactory();

    /**
     * {@inheritDoc}
     */
    @Override
    protected CaptchaFactory getCaptchaFactory() {
        return factory;
    }

    /**
     * 创建一个验证码工厂.
     *
     * @return 验证码工厂
     */
    protected GimpyFactory createFactory() {
        final Filter textFilter = BufferedImageOpFilter.create();
        final BackgroundBrush backgroundBrush = new ImageSourceBackgroundBrush();
        final TextBrush textBrush = new GlyphsBrush(MIN_TEXT_LENGTH, MAX_TEXT_LENGTH, new RandomColorGenerator(
                new Color(255, 255, 255),
                new Color(255, 220, 220),
                new Color(220, 255, 255),
                new Color(220, 220, 255),
                new Color(255, 255, 220),
                new Color(220, 255, 220)
        ), new GlyphsVisitor[]{
                new TranslateGlyphsVerticalRandomVisitor(5),
                new RotateGlyphsRandomVisitor(Math.PI / 32),
                new ShearGlyphsRandomVisitor(0.2, 0.2),
                new HorizontalSpaceGlyphsVisitor(4),
                new TranslateAllToRandomPointVisitor()
        }, new GlyphsDecorator[]{
                new RandomLinesGlyphsDecorator(1.2, new SingleColorGenerator(new Color(0, 0, 80)), 2, 25),
                new RandomLinesGlyphsDecorator(1, new SingleColorGenerator(new Color(238, 238, 238)), 1, 25)
        });
        final FontFamily fontFamily = new RandomFontFamily(MIN_FONT_SIZE, MAX_FONT_SIZE, new Font[]{
                new Font("nyala", Font.BOLD, MIN_FONT_SIZE),
                new Font("Arial", Font.BOLD, MIN_FONT_SIZE),
                new Font("Bell MT", Font.BOLD, MIN_FONT_SIZE),
                new Font("Credit valley", Font.BOLD, MIN_FONT_SIZE),
                new Font("Impact", Font.BOLD, MIN_FONT_SIZE)
        }, false);

        final Painter painter = new FilterablePainter(backgroundBrush, textBrush, fontFamily, textFilter);
        final RandomTextGenerator gen = new RandomTextGenerator("ABCDEGHJKLMNRSTUWXY235689");

        return new GimpyFactory(WIDTH, HEIGHT, painter, gen, false);
    }
}
